﻿#region Copyright
/*  This file is part of opinhost.
 *
 *  opinhost is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  opinhost is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with opinhost.  If not, see <http://www.gnu.org/licenses/>.
 */
#endregion
#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
#endregion

namespace server_daemon
{
    #region Daemon
    class Daemon
    {
        #region Create
        public static class Create
        {
            #region User
            public static void User(string username, string password)
            {
                //create user
                //Username:Password
                File.AppendAllText(Program.apppath + @"\server_files\users.sdf", username + ":" + password + Environment.NewLine);
            }
            #endregion

            #region Website
            public static void Website(string WebsiteName, string WebsiteShortName, string assignuser)
            {
                //create website
                //Website Name:Short Name:Assigned To User
                File.AppendAllText(Program.apppath + @"\server_files\data.sdf", WebsiteName + ":" + WebsiteShortName + ":" + assignuser);
                if (File.Exists(Program.apppath + @"server_files\websites") == false)
                {
                    Process crwebdi = new Process();
                    crwebdi.StartInfo.FileName = "makedir";
                    crwebdi.StartInfo.Arguments = @"server_files\websites";
                    crwebdi.StartInfo.CreateNoWindow = false;
                    crwebdi.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    crwebdi.Start();
                    Process crwebdir = new Process();
                    crwebdir.StartInfo.FileName = "makedir";
                    crwebdir.StartInfo.Arguments = @"server_files\websites\dbgwebsite";
                    crwebdir.StartInfo.CreateNoWindow = false;
                    crwebdir.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    crwebdir.Start();
                }
                else
                {
                    Process crwebdir = new Process();
                    crwebdir.StartInfo.FileName = "makedir";
                    crwebdir.StartInfo.Arguments = @"server_files\websites\dbgwebsite";
                    crwebdir.StartInfo.CreateNoWindow = false;
                    crwebdir.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    crwebdir.Start();
                }
            }
            #endregion
        }
        #endregion

        #region Delete
        public static class Delete
        {
            public static string response = "";
            public static void User(string username)
            {
                TextTool.AdvWriteLine("Deleting User: >" + username + "<", ConsoleColor.Green, true);
                string[] filelines = File.ReadAllLines(Program.apppath + @"\server_files\users.sdf");
                string filelines2write = null;
                int foundusers = 0;
                foreach (string line in filelines)
                {
                    string[] splitline = Regex.Split(line, ":");
                    string splitusername = splitline[0];
                    if (splitusername == username)
                    {
                        TextTool.AdvWriteLine("Found Username!", ConsoleColor.Green, true);
                        foundusers++;
                    }
                    else
                    {
                        filelines2write += line + "\r\n";
                    }
                }
                if (foundusers == 0)
                {
                    TextTool.AdvWriteLine("Could not find user!", ConsoleColor.Green, true);
                }
                else
                {
                    if (foundusers == 1)
                    {
                        response = "Deleted " + foundusers + " User.";
                        TextTool.AdvWriteLine("Found " + foundusers + " User.", ConsoleColor.Green, true);
                    }
                    else
                    {
                        response = "Deleted " + foundusers + " Users.";
                        TextTool.AdvWriteLine("Found " + foundusers + " Users.", ConsoleColor.Green, true);
                    }
                }
                Process dlusers = new Process();
                dlusers.StartInfo.FileName = "deletefile";
                dlusers.StartInfo.Arguments = @"server_files\users.sdf";
                dlusers.StartInfo.CreateNoWindow = false;
                dlusers.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                dlusers.Start();

                TextTool.AdvWriteLine("[START LINES TO WRITE]", ConsoleColor.Green, false);
                TextTool.AdvWriteLine(filelines2write, ConsoleColor.Green, false);
                TextTool.AdvWriteLine("[END LINES TO WRITE]", ConsoleColor.Green, false);
                TextTool.AdvWriteLine("Trying to Write New Users File...", ConsoleColor.Green, true);
                Console.ForegroundColor = ConsoleColor.Green;
                while (File.Exists(@"server_files\users.sdf") == true)
                {
                    Console.Write(".");
                }
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.White;
                File.AppendAllText(@"server_files\users.sdf", filelines2write);
            }
            public static void Website(string WebsiteShortName)
            {

            }
        }
        #endregion

        #region HTMLHandle
        public static class HTMLHandle
        {
            public static void Make(string website, string file, string content)
            {
                Process crdbgwebhtml = new Process();
                crdbgwebhtml.StartInfo.FileName = "makefile";
                crdbgwebhtml.StartInfo.Arguments = @"server_files\websites\" + website + @"\" + file + ".html";
                crdbgwebhtml.StartInfo.CreateNoWindow = false;
                crdbgwebhtml.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                crdbgwebhtml.Start();
                //wait 3 seconds until  everything is fine...
                int wsec = 0; //timer
                int wsed = 0; //time + 3secs
                bool wbol = true;
                wsed = 3 - 1;

                while (wbol == true)
                {
                    if (wsec == wsed)
                    {
                        wbol = false;
                    }
                    wsec++;
                    TextTool.AdvWriteLine("waiting " + wsec, ConsoleColor.Red, true);
                    System.Threading.Thread.Sleep(1000);
                }
                File.AppendAllText(@"server_files\websites\" + website + @"\" + file + ".html", content);
            }
            public static void Delete(string website, string file)
            {

            }
        }
        #endregion
    }
    #endregion
}
